package com.jd.tuning.controller;

import com.jd.tuning.jvm.async.AsyncDemoAsyncAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.AsyncRestTemplate;

import java.util.concurrent.ExecutionException;

@RestController
public class AsyncTestController {

    @Autowired
    private AsyncDemoAsyncAnnotation asyncDemoAsyncAnnotation;


    @Autowired
    private AsyncRestTemplate asyncRestTemplate;

    @GetMapping("/async")
    public String asyncTest() throws InterruptedException {
        asyncDemoAsyncAnnotation.biz();
        return "scuess";
    }


    @GetMapping("/a")
    public String a() throws InterruptedException, ExecutionException {
        ListenableFuture<ResponseEntity<String>> future=asyncRestTemplate.getForEntity("http://www.baidu.com",String.class);
//        ResponseEntity<Object> body= future.get();
        future.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
            @Override
            public void onFailure(Throwable throwable) {
                System.out.println("失败");
            }

            @Override
            public void onSuccess(ResponseEntity<String> objectResponseEntity) {
                        System.out.println(objectResponseEntity.getBody());
            }
        });
//        Object obj=body.getBody();
//        System.out.println(obj);
        return "scuess";
    }

}
